﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using BD.Standard.FOM.Program.Utils;
using BD.Standard.YCWH.ProgramParse.Utils;
using Newtonsoft.Json.Linq;

namespace BD.Standard.YCWH.ProgramParse.Forms
{
    public class Material
    {
        private static readonly string Logpath = @"UpdateToken\" + DateTime.Now.ToString("yyyyMM");

        public void PostMaterial(DataSet orgIdsSet)
        {
            try
            {
                if (orgIdsSet.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow orgIds in orgIdsSet.Tables[0].Rows)
                    {
                        string orgId = orgIds["orgId"].ToString();
                        int orgType = Convert.ToInt32(orgIds["orgType"].ToString());

                        //循环分页for,pageNo++
                        int pageNos = 2;
                        for (int pageNo = 1; pageNo <= pageNos; pageNo++)
                        {
                            long pageSize = 2000;
                            JObject page = new JObject()
                            {
                                {"pageNo",pageNo},
                                {"pageSize",pageSize},
                            };
                            JObject Requestbody = new JObject()
                            {
                                {"orgId",orgId},
                                {"status","1"},
                                {"page",page},
                            };
                            SignService signService = new SignService();
                            string json = Requestbody.ToString();

                            var formData = IntegrationUtils.getFormData(json, signService);


                            string posturlencoded = orgType == 1
                                ? new HttpUtils().PosturlencodedAsync(
                                    "https://api-open-cater.meituan.com/rms/scmplus/goods/api/v1/chain/goods/query", formData)
                                : new HttpUtils().PosturlencodedAsync(
                                    "https://api-open-cater.meituan.com/rms/scmplus/goods/api/v1/poi/goods/query", formData);

                            var result = IntegrationUtils.getPage(posturlencoded, pageSize, out pageNos);


                            // ReSharper disable once PossibleNullReferenceException
                            if (result["code"].ToString().Equals("OP_SUCCESS"))
                            {
                                // ReSharper disable once PossibleNullReferenceException
                                MidTableData.InsertTable("MATERIAL", "items", result["data"].ToString());
                            }
                        }
                    }

                }
            }
            catch (Exception e)
            {
                Logger logger = new Logger(ConfigurationManager.AppSettings["log"] + Logpath, DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
                logger.WriteLog("数据出现异常,错误信息：" + e.Message);
                logger.WriteLog("             堆栈信息：" + e.StackTrace);
                Console.WriteLine(e.Message);
            }
            
        }

       
    }
}
